package selenium;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

/**
 * @author Tower
 * @version Jun 3, 2008 2:40:54 PM
 */
public class 代理 {

	public void setLocalProxy() {
		Properties prop = System.getProperties();
		prop.setProperty("http.proxyHost", "222.222.222.222");
		prop.setProperty("http.proxyPort", "3128");

	}

	// 清除proxy设置
	public void removeLocalProxy() {
		Properties prop = System.getProperties();
		prop.remove("http.proxyHost");
		prop.remove("http.proxyPort");
		prop.remove("http.nonProxyHosts");
		prop.remove("https.proxyHost");
		prop.remove("https.proxyPort");
		prop.remove("ftp.proxyHost");
		prop.remove("ftp.proxyPort");
		prop.remove("ftp.nonProxyHosts");
		prop.remove("socksProxyHost");
		prop.remove("socksProxyPort");
	}

	// 测试HTTP代理
	// public void showHttpProxy(Object... proxy) {
	public void showHttpProxy(Object... proxy) {
		getV4IP();
		URL url = null;
		try {
			url = new URL("http://ip.chinaz.com/getip.aspx");
		} catch (MalformedURLException e) {
			return;
		}
		try {
			URLConnection conn = null;
			switch (proxy.length) {
			case 0:
				conn = url.openConnection();
				break;
			case 1:
				conn = url.openConnection((Proxy) proxy[0]);
				break;
			default:
				break;
			}
			if (conn == null)
				return;
			conn.setConnectTimeout(10000); // 设置连接超时时间
			InputStream in = conn.getInputStream();
			BufferedReader br = new BufferedReader(new java.io.InputStreamReader(in));
			String str = null;
			try {
				while ((str = br.readLine()) != null) {
					System.out.println(str);
					/*
					 * if(str.indexOf("百度一下，你就知道")>0){ break; }
					 */
				}

			} catch (IOException e1) {
			}
		} catch (IOException e1) {
			return;
		}
	}

	// 得到一个proxy
	public static Proxy getProxy(String host, int port) {
		SocketAddress addr = new InetSocketAddress(host, port);
		Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);
		return typeProxy;
	}

	/*
	 * // 得到一个proxy public Proxy getProxy(Proxy.Type type, String host, int
	 * port) { SocketAddress addr = new InetSocketAddress(host, port); Proxy
	 * typeProxy = new Proxy(type, addr); return typeProxy; }
	 */
	public void test() {
		String pathurl = "d:/temp.txt";
		try {
			File file = new File(pathurl);
			FileInputStream fin = new FileInputStream(file);
			BufferedInputStream bin = new BufferedInputStream(fin);
			BufferedReader ir = new BufferedReader(new InputStreamReader(bin));
			String str = null;
			int count = 0;
			StringBuffer ip = new StringBuffer();
			while ((str = ir.readLine()) != null) {

				String ipfilter = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";

				Pattern p = Pattern.compile(ipfilter);
				Matcher m = p.matcher(str);
				if (m.find()) {
					count++;
					String port = str.substring(str.indexOf(":") + 1, str.length());
					if (port.indexOf("0") == 0) {
						port = port.substring(2, port.length());
					}
					String host = m.group();
					int po = Integer.parseInt(port);
					// System.out.println(host+"--"+port);
					System.out.println("----------" + count);
					showHttpProxy(getProxy(host, po));
				}
				// 获取端口号

			}
			/*
			 * System.out.println(ip); java.io.FileWriter fileWriter = new
			 * java.io.FileWriter(pathurl); fileWriter.write(ip.toString());
			 * fileWriter.close(); bin.close(); fin.close();
			 */
		} catch (Exception e) {

		}
		// showHttpProxy(getProxy("58.211.78.143",80));
	}

	public static void main(String[] args) {
		代理 proxy = new 代理();
		// proxy.setLocalProxy();
		proxy.showHttpProxy(getProxy("114.115.140.25", 3128));// 114.115.140.25:3128

	}

	public static String getV4IP() {
		String chinaz = "http://ip.chinaz.com/getip.aspx";
		String inputLine = new String();
		String read = "";
		URL url = null;
		HttpURLConnection urlConnection = null;
		BufferedReader in = null;
		try {
			url = new URL(chinaz);
			urlConnection = (HttpURLConnection) url.openConnection();
			in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
			while ((read = in.readLine()) != null) {
				JSONObject userObj = new JSONObject(read);
				inputLine = userObj.optString("ip");
				System.out.println("IP所在地：" + userObj.optString("address"));
			}
			// System.out.println(inputLine.toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return inputLine;
	}

}
